package com.example.materialviewpagerdemo;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.materialviewpagerdemo.Presenter.CurrentDataController;
import com.example.materialviewpagerdemo.bean.Dezhou;
import com.example.materialviewpagerdemo.bean.StaticData;
import com.example.materialviewpagerdemo.util.ChartUtil;
import com.example.materialviewpagerdemo.util.LoadingUtil;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.zyao89.view.zloading.ZLoadingDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ChartActivity2 extends AppCompatActivity {
    LoadingUtil loadingUtil;
    Typeface typeface;
    @BindView(R.id.lineChart)
    LineChart lineChart;
    List<Entry> list = new ArrayList<>();
    List<BarEntry> barlist = new ArrayList<>();

    ZLoadingDialog zLoadingDialog;
    @BindView(R.id.chart_pm_one)
    BarChart chartPmOne;


    @BindView(R.id.chart_name)
    TextView chartName;
    @BindView(R.id.chart_currentdata)
    TextView chartCurrentdata;
    @BindView(R.id.unit)
    TextView unit;
    @BindView(R.id.lineChart_name)
    TextView lineChartName;
    @BindView(R.id.barchart_name)
    TextView barchartName;
    @BindView(R.id.card_view_barchart)
    CardView cardViewBarchart;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chart);
        ButterKnife.bind(this);
        zLoadingDialog = new ZLoadingDialog(this);
        loadingUtil = new LoadingUtil(zLoadingDialog);
        loadingUtil.showDialog();
        String flag = (String) getIntent().getExtras().get("flag");
        if ("1".equals(flag)) {
            setTextData("1");
            getDataList(7, "1");
        } else if ("2".equals(flag)) {
            setTextData("2");
            getDataList(7, "2");
        } else if ("3".equals(flag)) {
            setTextData("3");
            getDataList(7, "3");
        } else if ("4".equals(flag)) {
            setTextData("4");
            getDataList(7, "4");
        }else if ("5".equals(flag)) {
            setTextData("5");
            getDataList(7, "5");
        }
        else if ("6".equals(flag)) {
            setTextData("6");
            getDataList(7, "6");
        }
        else if ("7".equals(flag)) {
            setTextData("7");
            getDataList(7, "7");
        }
        else if ("8".equals(flag)) {
            setTextData("8");
            getDataList(7, "8");
        }
        else if ("8".equals(flag)) {
            setTextData("8");
            getDataList(7, "8");
        }


        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    private void getDataList(int number, String flag) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(StaticData.url)
                .build();
        CurrentDataController currentDataController = retrofit.create(CurrentDataController.class);
        Call<List<Dezhou>> call = currentDataController.query(number, "004");
        call.enqueue(new Callback<List<Dezhou>>() {
            @Override
            public void onResponse(Call<List<Dezhou>> call, Response<List<Dezhou>> response) {
                if (response.body() != null) {
                    ListIterator<Dezhou> listIterator = null;
                    try {
                        listIterator = response.body().listIterator();
                    } catch (NullPointerException e) {
                        return;
                    }
                    int i = 0;
                    while (listIterator.hasNext()) {
                        i++;
                        Dezhou currentData = listIterator.next();
                        addDataList(currentData, i);
                    }

                    Collections.reverse(list);
                    Collections.reverse(barlist);
                    ChartUtil.setChartData(lineChart, list);
                    ChartUtil.setBarChartData(chartPmOne, barlist);
                    loadingUtil.cancleDialog();
                }


            }

            @Override
            public void onFailure(Call<List<Dezhou>> call, Throwable t) {
                loadingUtil.cancleDialog();
                System.out.println("失败");
            }
        });
    }

    public void addDataList(Dezhou currentData, int i) {
        Entry entry = null;
        BarEntry barEntry = null;
        i = 10 - i;
        switch ((int) getIntent().getExtras().get("id")) {

            case 1:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAir_temp1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAir_temp1()), ChartUtil.praseData(currentData.getDatetime()));
                break;
            case 2:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirHumi1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirHumi1()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 3:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirTemp2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirTemp2()), ChartUtil.praseData(currentData.getDatetime()));
                break;
            case 4:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getAirHumi2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getAirHumi2()), ChartUtil.praseData(currentData.getDatetime()));

                break;

            case 5:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getCo2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getCo2()), ChartUtil.praseData(currentData.getDatetime()));

                break;

            case 6:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSunLight1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSunLight1()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 7:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSunLight2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSunLight2()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 8:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilHumi1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilHumi1()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 9:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilSalt1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilSalt1()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 10:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilTemp1()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilTemp1()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 11:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilHumi2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilHumi2()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 12:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilSalt2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilSalt2()), ChartUtil.praseData(currentData.getDatetime()));

                break;
            case 13:
                entry = new Entry(i, ChartUtil.toFloat(currentData.getSoilTemp2()), ChartUtil.praseData(currentData.getDatetime()));
                barEntry = new BarEntry(i, ChartUtil.toFloat(currentData.getSoilTemp2()), ChartUtil.praseData(currentData.getDatetime()));

                break;
        }
        list.add(entry);
        barlist.add(barEntry);

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void setTextData(String flag) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(StaticData.url)
                .build();
        CurrentDataController currentDataController = retrofit.create(CurrentDataController.class);
        Call<Dezhou> call = currentDataController.queryOne("004");
        call.enqueue(new Callback<Dezhou>() {
            @Override
            public void onResponse(Call<Dezhou> call, Response<Dezhou> response) {
                try {
                    setData(response);
                } catch (NullPointerException e) {
                    Toast.makeText(ChartActivity2.this, "数据获取失败,请检查网络状态", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(Call<Dezhou> call, Throwable t) {

            }
        });


    }


    public void setData(Response<Dezhou> response) {
        switch ((int) getIntent().getExtras().get("id")) {
            case 0:
                break;
            case 1:
                unit.setText("℃");
                setRepeatName("空气温度");
                chartCurrentdata.setText(response.body().getAir_temp1());
                break;
            case 2:
                unit.setText("%");
                setRepeatName("空气湿度");
                chartCurrentdata.setText(response.body().getAirHumi1());
                break;
            case 3:
                unit.setText("℃");
                setRepeatName("空气温度");
                chartCurrentdata.setText(response.body().getAirTemp2());
                break;
            case 4:
                unit.setText("%");
                setRepeatName("空气湿度");
                chartCurrentdata.setText(response.body().getAirHumi2());
                break;
            case 5:
                unit.setText("ppm");
                setRepeatName("CO2");
                chartCurrentdata.setText(response.body().getCo2());
                break;
            case 6:
                unit.setText("kLux");
                setRepeatName("阳光1");
                chartCurrentdata.setText(response.body().getSunLight1());
                break;
            case 7:
                unit.setText("kLux");
                setRepeatName("阳光2");
                chartCurrentdata.setText(response.body().getSunLight2());
                break;
            case 8:
                unit.setText("%");
                setRepeatName("土壤湿度1");
                chartCurrentdata.setText(response.body().getSoilHumi1());
                break;
            case 9:
                unit.setText("µs/m");
                setRepeatName("土壤电导率1");
                chartCurrentdata.setText(response.body().getSoilSalt1());
                break;
            case 10:
                unit.setText("℃");
                setRepeatName("土壤温度1");
                chartCurrentdata.setText(response.body().getAir_temp1());
                break;
            case 11:
                unit.setText("%");
                setRepeatName("土壤湿度2");
                chartCurrentdata.setText(response.body().getSoilHumi2());
                break;
            case 12:
                unit.setText("µs/m");
                setRepeatName("土壤电导率2");
                chartCurrentdata.setText(response.body().getSoilSalt2());
                break;
            case 13:
                unit.setText("℃");
                setRepeatName("土壤温度2");
                chartCurrentdata.setText(response.body().getAirTemp2());
                break;



        }


    }

    public void setRepeatName(String name) {
        chartName.setText("实时" + name);
        lineChartName.setText(name + "-折线图");
        barchartName.setText(name + "-柱状图");
    }
}

